
<!--搜索框-->
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
    <div class="layui-form-item">
        <div class="layui-inline">
            <input type="text" name="role_name" placeholder="角色名称" autocomplete="off" class="layui-input" value="">
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin layui-btn-primary" lay-submit lay-filter="LAY-user-back-search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
            </button>
        </div>
        {if check_roles('admin/user_role/create')}
            <div class="layui-inline">
                <button class="layui-btn top_toolbar layui-btn-primary" data-target="iframe" data-type="add">
                    <i class="icon iconfont icon-tianjia"></i> 添加
                </button>
            </div>
        {/if}
    </div>
</div>

<div class="layui-card-body">
    <table id="demo" lay-filter="test"></table>
    <!--行功能-->
    <script type="text/html" id="row_operation">
        {{#  if(d.is_lock == 1){ }}
        {if check_roles('admin/user_role/edit')}
            <a class="layui-btn layui-btn-disabled layui-btn-xs" >编辑</a>
        {/if}
        {if check_roles('admin/user_role/delete')}
            <a class="layui-btn layui-btn-disabled layui-btn-xs">删除</a>
        {/if}
        {{#  } else { }}
        {if check_roles('admin/user_role/edit')}
            <a class="layui-btn layui-btn-normal-outline layui-btn-xs" lay-event="edit">编辑</a>
        {/if}
        {if check_roles('admin/user_role/delete') }
            <a class="layui-btn layui-btn-danger-outline layui-btn-xs" lay-event="del">删除</a>
        {/if}
        {{#  } }}
    </script>
</div>
<script>
    layui.use('table',function () {
        var table = layui.table,form = layui.form;

        //初始化表格
        var tableIns = table.render({
            elem: '#demo'
            ,url: '/admin/user_role/index/' //数据接口
            ,page: true //开启分页
            ,limit: 15
            ,cols: [[ //表头
                {field: 'role_name', title: '角色名称',align:'center'}
                // ,{field: 'range_alias', title: '职责范围。',align:'center'}
                ,{field: 'description', title: '描述',align:'center'}
                ,{field: 'create_time', title: '创建时间',align:'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#row_operation', width:200,align:'center'}
            ]]
        });

        //监听搜索
        form.render(null, 'layadmin-useradmin-formlist');
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            tableIns.reload({
                where: field
                ,page: {
                        curr: 1 //重新从第 1 页开始
                }
            });
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(obj);
            if(obj.event === 'del'){
                layer.confirm('确定删除['+data['role_name']+']？', function(index){
                    $.post("{:url('user_role/delete')}",{id:data.id},function (ret) {
                        if(ret.code == 0){
                            layui.table.reload('demo');
                            layer.msg(ret.msg,{icon:6,time:1000})
                        }else{
                            layer.msg(ret.msg,{icon:5,time:2000})
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,content: "{:url('user_role/edit')}?id="+data.id
                    ,shadeClose: true
                    ,shade: 0.4
                    ,area: ['500px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#form-submit");
                        submit.click();
                    }
                });
            }
        });
    });

    //头部功能条
    $('.top_toolbar').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //事件对象
    var active = {
        add: function(){
            layer.open({
                type: 2,
                title: '添加',
                shadeClose: true,
                shade: 0.4,
                area: ['500px', '600px'],
                content: '/admin/user_role/create', //iframe的url
                btn: ['确认', '取消'],
                yes:function(index, layero){
                    //按钮【按钮一】的回调
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#form-submit");
                    submit.click();
                },
                btn2:function(index,layero){
                    //按钮【按钮二】的回调
                    layer.close(index);
                }
            });
        }
    }
</script>
